package com.sky.controller.admin;


import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import com.sky.dto.GetScripsDTO;
import com.sky.dto.SaveOrUpdataScriptDTO;
import com.sky.entity.Scripts;
import com.sky.result.Result;
import com.sky.service.ScriptsService;
import com.sky.vo.GetScripsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author majun
 * @package com.sky.controller.admin
 * @date 2025/6/21 12:09
 */

@RestController
@RequestMapping("/admin/scripts")
@Slf4j
public class ScriptsController {

    @Autowired
    private ScriptsService scriptsService;

    /**
     * 新增或修改剧本
     *
     * @param saveOrUpdataScriptDTO 剧本数据
     * @return 响应结果
     */
    @PostMapping("/save")
    private Result saveScripts(@RequestBody SaveOrUpdataScriptDTO saveOrUpdataScriptDTO) {

        log.info("新增或修改剧本：{}", saveOrUpdataScriptDTO);

        scriptsService.saveOrUpdateScript(saveOrUpdataScriptDTO);

        return Result.success();
    }

    /**
     * 分页获取剧本
     *
     * @return 响应结果
     */
    @PostMapping("/page")
    private Result<Pager<GetScripsVO>> getScrips(@RequestBody GetScripsDTO getScripsDTO) {

        log.info("分页获取剧本");

        Pager<GetScripsVO> pager = scriptsService.getScrips(getScripsDTO);

        return Result.success(pager);

    }

    /**
     * 删除剧本
     * @param id 剧本ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    private Result<String> deleteScript(@PathVariable Long id) {
        log.info("删除剧本, id: {}", id);
        try {
            scriptsService.deleteScript(id);
            return Result.success("删除剧本成功");
        } catch (Exception e) {
            log.error("删除剧本失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

}
